//
//  MainViewController.m
//  WYProject
//
//  Created by dihuijun on 17/4/17.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "MainViewController.h"
#import "ScienceViewController.h"
#import "SocietyViewController.h"
#import "TopLineViewController.h"
#import "VideoViewController.h"
#import "ReaderViewController.h"
#import "HotViewController.h"

#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
//label宽度
static CGFloat const lblWidth  = 100.0;
static CGFloat const radio     = 1.2;
static CGFloat const lblheight = 44.0;//标题label高度
static CGFloat const lineViewHeight = 2.0;//分割线高度

@interface MainViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *titleScrollView;
@property (weak, nonatomic) IBOutlet UIScrollView *bodyScrollView;
///切换状态
@property (nonatomic, weak) UILabel *selLabel;
/**   储存label   */
@property (nonatomic, strong) NSMutableArray *lblArray;
/**   label   */
@property (nonatomic, strong) UIView *lineview;
@end

@implementation MainViewController
/**   lblArray   */
- (NSMutableArray *)lblArray{
    if(!_lblArray){
        _lblArray = [NSMutableArray array];
    }
    return _lblArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ///1.创建子控制器
    [self setupChildViewControllers];
    ///2.设置标签
    [self setupTitle];
    //iOS7会给导航控制器下的所有UIScrollView添加额外的滚动区域
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    ///3.初始化scrollView
    [self initScrollView];
  
}

#pragma mark --- 1.创建子控制器
- (void)setupChildViewControllers
{
    //头条
    TopLineViewController *topLineVC = [[TopLineViewController alloc]init];
    topLineVC.title                  = @"头条";
    [self addChildViewController:topLineVC];
    
    
    //热点
    HotViewController *HotVC         = [[HotViewController alloc]init];
    HotVC.title                      = @"热点";
    [self addChildViewController:HotVC];
    
    
    //视屏
    VideoViewController *VideoVC     = [[VideoViewController alloc]init];
    VideoVC.title                    = @"视屏";
    [self addChildViewController:VideoVC];
    
    
    //社会
    SocietyViewController *socityVC  = [[SocietyViewController alloc]init];
    socityVC.title                   = @"分享社会";
    [self addChildViewController:socityVC];
    
    
    //阅读
    ReaderViewController *ReaderVC   = [[ReaderViewController alloc]init];
    ReaderVC.title                   = @"阅读";
    [self addChildViewController:ReaderVC];
    
    
    //科技
    ScienceViewController *scienceVC = [[ScienceViewController alloc]init];
    scienceVC.title                  = @"科技";
    [self addChildViewController:scienceVC];
    
    
    
    
    
    
    
}
#pragma mark --- 2.设置标签
- (void)setupTitle{
    //分割线
    self.lineview = [UIView new];
    self.lineview.backgroundColor = [UIColor redColor];
    [self.titleScrollView addSubview:_lineview];
    
    ///移除干扰
    [self.lblArray removeAllObjects];
    
    
    
    NSUInteger count = self.childViewControllers.count;
    
   
    CGFloat lblX      = 0;
    CGFloat lblY      = 0;
    
    
    for (NSInteger i=0; i<count; i++) {
        UIViewController *vc = self.childViewControllers[i];
        //1.创建label
        UILabel *label = [UILabel new];
        
        [self.lblArray addObject:label];
        //2.设置title
        label.text     = vc.title;
        label.tag      = i;
        //3.设置frame
        lblX           = i*lblWidth;
        label.frame    = CGRectMake(lblX, lblY, lblWidth, lblheight);
        label.textAlignment = NSTextAlignmentCenter;
        //4.添加手势
        label.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
        [label addGestureRecognizer:tap];
        //第一个选中
        if (i==0) {
            [self titleClick:tap];
        }
        //设置高亮文字颜色
        label.highlightedTextColor = [UIColor redColor];
        //5.添加到scrollView
        [self.titleScrollView addSubview:label];
        
    }
    
    
}
/**  点击按钮 */
- (void)titleClick:(UITapGestureRecognizer *)tap
{
    
    UILabel *selLabel  = (UILabel *)tap.view;
    
    //分割线
    //分割线
    CGFloat width = [selLabel.text sizeWithAttributes:@{NSFontAttributeName:selLabel.font}].width;
    CGFloat centerX = selLabel.center.x;
    self.lineview.frame = CGRectMake(centerX-width*0.5, lblheight-lineViewHeight, width, lineViewHeight);
    //1.标题颜色变成红色，设置高亮状态下的颜色
    [self selectedLabel:selLabel];
    
    //2.滚动到对应的位置
    NSInteger index    = selLabel.tag;
    
    //2.1 计算滚动位置
    
    CGFloat   OffsetX  = index*screenW;
    self.bodyScrollView.contentOffset = CGPointMake(OffsetX, 0);//偏移
    
    //3.给对应的位置添加控制器
    [self showVC:index];
    ///4.设置title居中
    [self centerLabel:selLabel];
    
}
- (void)selectedLabel:(UILabel *)label
{
    //1.1 取消高亮
    _selLabel.highlighted = NO;
    //1.2 把颜色设为初始颜色
    _selLabel.textColor = [UIColor blackColor];
    //1.3 形变还原
    _selLabel.transform = CGAffineTransformIdentity;
    
    
    //2.1 设置选中状态高亮
    label.highlighted = YES;
    //2.2 设置形变
    label.transform = CGAffineTransformMakeScale(radio, radio);
    
    
    //3.赋值
    _selLabel = label;
}

#pragma mark --- 3.初始化scrollView
- (void)initScrollView
{
    self.titleScrollView.showsVerticalScrollIndicator = NO;
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
    
    //title个数
    NSUInteger titleCount = self.childViewControllers.count;
    self.titleScrollView.contentSize = CGSizeMake(titleCount*lblWidth, 0);
    
    //处理内容滚动条
    self.bodyScrollView.contentSize = CGSizeMake(titleCount*screenW, 0);
    self.bodyScrollView.pagingEnabled = YES;//开启分页
    self.bodyScrollView.bounces = NO;//无弹簧效果
    self.bodyScrollView.showsHorizontalScrollIndicator = NO;
    ///设置内容滚动的代理
    self.bodyScrollView.delegate = self;
}
#pragma mark  ------- scrollViewDelegate代理
/**  当滚动完成 */
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  
    //1.得到页数
    NSUInteger pageIndex = self.bodyScrollView.contentOffset.x / self.bodyScrollView.bounds.size.width;
    //3.1添加对应控制器
    [self showVC:pageIndex];
    //3.2 切换标题为选中状态
    UILabel *selctedLabel = self.lblArray[pageIndex];
    [self selectedLabel:selctedLabel];
    ///4.设置title居中
    [self centerLabel:selctedLabel];
    
    
    
    //分割线
    CGFloat width = [selctedLabel.text sizeWithAttributes:@{NSFontAttributeName:selctedLabel.font}].width;
    CGFloat centerX = selctedLabel.center.x;
    self.lineview.frame = CGRectMake(centerX-width*0.5, lblheight-lineViewHeight, width, lineViewHeight);
    

}



/**
 *  显示控制器
 */
- (void)showVC:(NSInteger)index
{
    CGFloat   OffsetX    = index*screenW;
    UIViewController *vc = self.childViewControllers[index];

    //判断控制器View有没有加载过，加载过就不需要添加了
    if (vc.isViewLoaded) return;
    
    vc.view.frame        = CGRectMake(OffsetX, 0, self.bodyScrollView.bounds.size.width, self.bodyScrollView.bounds.size.height);
    
    
    [self.bodyScrollView addSubview:vc.view];
    
    
}

/**
 *  居中titleLabel
 */
- (void)centerLabel:(UILabel *)centerLabel
{
    
    //1计算偏移量
    CGFloat offsetX = centerLabel.center.x - screenW*0.5;
    
    //1.1 左边 前2个，不能居中显示的,也就是offsetX可能为负数
//    NSLog(@"%f  %f  %f  %f",centerLabel.center.x,screenW*0.5,screenW,offsetX);
    
    if (offsetX<0)offsetX=0;
    
    //1.2 最右边的，也就是最后一个label不能距离左边太远
    CGFloat maxOffSetX = self.titleScrollView.contentSize.width - screenW;
    
    if (offsetX > maxOffSetX) offsetX = maxOffSetX;
    
    //滚动标题滚动条
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
//    NSLog(@"%f  %f  %f",maxOffSetX,self.titleScrollView.contentSize.width,offsetX);

 
    

}



/*****  监听scrollView滚动*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f",scrollView.contentOffset.x);
    
    //让文字的大小和颜色 渐变一下
    
    //1.1 得到页数
//    CGFloat currentPage = scrollView.contentOffset.x / scrollView.bounds.size.width;
////    NSLog(@"%f",currentPage);
//    
//    
//    NSInteger leftIndex = currentPage;
//    //1.2 划过去后第二个label
//    NSInteger rightIndex = leftIndex+1;
//    
//    //1.3 取出左边的label
//    UILabel *leftLabel = self.lblArray[leftIndex];
//    
//    //1.4 取出右边的label(可能会越界，需要判断下)
//    UILabel *rightLabel;
//    if (rightIndex<self.lblArray.count-1) {
//        rightLabel = self.lblArray[rightIndex];
//    }
//    
//    
//    //2.计算缩放比例
//    CGFloat rightScale = currentPage-leftIndex;
//    CGFloat leftScale = 1-rightScale;
//    //3.开始缩放
//    leftLabel.transform = CGAffineTransformMakeScale(leftScale*0.3+1, leftScale*0.3+1);
//    rightLabel.transform = CGAffineTransformMakeScale(rightScale*0.3+1, rightScale*0.3+1);
//    
//    //4.颜色渐变
//    /*
//      黑色RGB：0 0 0 
//      红色RGB：1 0 0
//     */
//    leftLabel.textColor = [UIColor colorWithRed:leftScale green:0 blue:0 alpha:1];
//    rightLabel.textColor = [UIColor colorWithRed:rightScale green:0 blue:0 alpha:1];
//    
//    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**  希望 滚动到第二个的时候第一个标题 由大变小，颜色由第一个标题红色变成黑色，第二个标题黑色变红色*/
    
    
    //1.1 得到页数
    CGFloat page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    //1.2 得到左边的index
    NSInteger leftIndex1 = page;
    
    //1.3 得到右边
    NSInteger rightIndex2 = leftIndex1+1;
    
    //2.1 取出左边label
    UILabel *leftLbl = self.lblArray[leftIndex1];
    //2.2 取出右边的label(不越界的情况下)
    
    UILabel *rightLbl;
    if (rightIndex2<self.lblArray.count-1) {
        rightLbl = self.lblArray[rightIndex2];
    }
    
    //3.计算缩放比例（第一个由大变小）
    CGFloat rightScale2 = page-leftIndex1;
    CGFloat leftScale1 = 1-rightScale2;
    
    leftLbl.transform = CGAffineTransformMakeScale(leftScale1*0.2+1, leftScale1*0.2+1);
    rightLbl.transform = CGAffineTransformMakeScale(rightScale2*0.2+1, rightScale2*0.2+1);
    
    //4.颜色
    leftLbl.textColor = [UIColor colorWithRed:leftScale1 green:0 blue:0 alpha:1];
    rightLbl.textColor = [UIColor colorWithRed:rightScale2 green:0 blue:0 alpha:1];
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}






@end
